#!/bin/bash
# docker-entrypoint.sh

set -e

echo "开始初始化 RuoYi Node.js 应用环境..."

# 确保日志和上传目录存在且有正确的权限
echo "检查日志和上传目录权限..."
mkdir -p dist/logs dist/uploads
touch dist/logs/.keep dist/uploads/.keep
chmod -R 755 dist/logs dist/uploads

# 等待MySQL就绪，使用环境变量或默认值
echo "等待MySQL数据库就绪..."
./wait-for-it.sh ${DB_HOST:-mysql} ${DB_PORT:-3306}

# 等待Redis就绪
echo "等待Redis服务就绪..."
./wait-for-it.sh ${REDIS_HOST:-redis} ${REDIS_PORT:-6379}

# 检查环境变量
echo "检查环境变量配置..."
if [ -z "$DB_PASSWORD" ]; then
  echo "警告: 未设置DB_PASSWORD环境变量，将使用默认值"
fi

if [ -z "$REDIS_PASSWORD" ]; then
  echo "警告: 未设置REDIS_PASSWORD环境变量，将使用默认值"
fi

if [ -z "$JWT_SECRET" ]; then
  echo "警告: 未设置JWT_SECRET环境变量，将使用默认值"
fi

# 显示Node.js版本信息
echo "Node.js版本:"
node --version
echo "NPM版本:"
npm --version
echo "PNPM版本:"
pnpm --version

# 切换到dist目录
cd dist

# 启动应用
echo "启动RuoYi Node.js应用..."
exec node app.js 